package cn.com.dpc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.dpc.adapter.ColumnAdapter;
import cn.com.dpc.bean.Columnobject;
import cn.com.dpc.bean.User;
import cn.com.dpc.util.DataService;
import cn.com.dpc.util.HttpGet;

public class ParamsActivity extends BaseListActivity {
	private int stationId;
	private int devId;
	private String devName = "";
	private String resultStr = "";

	ListView lv;

	private List<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
	private SimpleAdapter m_adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.params);

		Bundle bundle = getIntent().getExtras();
		String url = bundle.getString("url");
		devName = bundle.getString("text");
		stationId = Integer.parseInt(url.split("_")[0]);
		devId = Integer.parseInt(url.split("_")[1]);

		ImageButton editButton = (ImageButton) findViewById(R.id.edit_button);
		editButton.setVisibility(View.INVISIBLE);
		ImageButton backButton = (ImageButton) findViewById(R.id.back_button);
		backButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				ParamsActivity.this.finish();
			}
		});

		TextView titleText = (TextView) findViewById(R.id.myTitle);
		titleText.setText(devName);

		showList();

	}

	private void showList() {
		new AsyncTask<Void, Void, Boolean>() {

			@Override
			protected Boolean doInBackground(Void... arg0) {
				try {
					User user = DataService.queryUser(ParamsActivity.this);
					Log.v("dpc.ParamsActivity.resultStr0",
							"http://" + user.getIp()
									+ "/andriod!findParams.do?stationNo="
									+ stationId + "&devNo=" + devId);
					resultStr = HttpGet.getResult("http://" + user.getIp()
							+ "/andriod!findParams.do?stationNo=" + stationId
							+ "&devNo=" + devId, ParamsActivity.this, "utf8");
					Log.v("dpc.ParamsActivity.resultStr", resultStr);
					return true;
				} catch (Exception e) {
					e.printStackTrace();
				}
				return false;
			}

			@Override
			protected void onPostExecute(Boolean result) {
				super.onPostExecute(result);
				closeProgressBar();
				if (result) {

					try {
						// 每行的数据
						JSONObject jsonObject = new JSONObject(resultStr.trim());
						JSONArray jsonArray = jsonObject.getJSONArray("datas");
						Log.v("dpc.ParamsActivity.jsonArrayLength",
								jsonArray.length() + " ");
						for (int i = 0; i < jsonArray.length(); i++) {
							JSONObject jo = jsonArray.getJSONObject(i);

							HashMap<String, Object> map = new HashMap<String, Object>();
							if (jo.has("name")) {
								map.put("name", jo.getString("name"));
							} else {
								map.put("name", " ");
							}
							if (jo.has("value")) {
								map.put("value", jo.getString("value"));
							} else {
								map.put("value", " ");
							}
							if (jo.has("dateTime")) {
								map.put("time", jo.getString("dateTime"));
							} else {
								map.put("time", " ");
							}
							listItem.add(map);
						}

						m_adapter = new SimpleAdapter(ParamsActivity.this,
								listItem,// 数据源
								R.layout.listviewparams,// ListItem的XML实现
								// 动态数组与ImageItem对应的子项
								new String[] { "name", "value", "time" },
								// ImageItem的XML文件里面的一个ImageView,两个TextView ID
								new int[] { R.id.name, R.id.value, R.id.time });

						setListAdapter(m_adapter);

					} catch (Exception e) {
						e.printStackTrace();
					}

				} else {
					Toast.makeText(ParamsActivity.this, "数据加载失败！",
							Toast.LENGTH_SHORT).show();
				}

			}

			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				showProgressBar();
			}

		}.execute();

	}

	private void changeObject(List<Columnobject> list,
			ColumnAdapter columnAdapter) {
		int[] number = new int[list.size()];
		int[] type = new int[list.size()];
		String[] url = new String[list.size()];
		String[] texts = new String[list.size()];
		for (int i = 0; i < list.size(); i++) {
			Columnobject columnobject = list.get(i);
			number[i] = columnobject.getNumber();
			type[i] = columnobject.getType();
			url[i] = columnobject.getUrl();
			texts[i] = columnobject.getTexts();
		}
		columnAdapter.setNumber(number);
		columnAdapter.setType(type);
		columnAdapter.setUrl(url);
		columnAdapter.setTexts(texts);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			this.finish();
			return true;
		}
		return true;
	}

	public void showProgressBar() {
		AnimationSet set = new AnimationSet(true);

		Animation animation = new AlphaAnimation(0.0f, 1.0f);
		animation.setDuration(500);
		set.addAnimation(animation);

		animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
				Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
				-1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
		animation.setDuration(500);
		set.addAnimation(animation);

		LayoutAnimationController controller = new LayoutAnimationController(
				set, 0.5f);
		RelativeLayout loading = (RelativeLayout) findViewById(R.id.loading);
		loading.setVisibility(View.VISIBLE);
		loading.setLayoutAnimation(controller);
	}

	public void closeProgressBar() {

		AnimationSet set = new AnimationSet(true);

		Animation animation = new AlphaAnimation(0.0f, 1.0f);
		animation.setDuration(500);
		set.addAnimation(animation);

		animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
				Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
				0.0f, Animation.RELATIVE_TO_SELF, -1.0f);
		animation.setDuration(500);
		set.addAnimation(animation);

		LayoutAnimationController controller = new LayoutAnimationController(
				set, 0.5f);
		RelativeLayout loading = (RelativeLayout) findViewById(R.id.loading);

		loading.setLayoutAnimation(controller);

		loading.setVisibility(View.INVISIBLE);
	}

	public void showProgressBar(String title) {
		TextView loading = (TextView) findViewById(R.id.txt_loading);
		loading.setText(title);
		showProgressBar();
	}
}
